﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WorldView
{

    public partial class ChestWindow : Form
    {
        Varies v;
        Boolean isFirst = true;
        Boolean reset = false;
        public ChestWindow(Varies v1)
        {
            this.v = v1;
            InitializeComponent();
            for (int i = 0; i < 1000; i++)
            {
                
                Chests.Items.Add(v.chests[i].id);

            }
        }

        private void ChestWindow_Load(object sender, EventArgs e)
        {

        }

        private void Chests_SelectedIndexChanged(object sender, EventArgs e)
        {
            XBox.Enabled = true;
            YBox.Enabled = true;
            if (v.chests[Chests.SelectedIndex].X != -1 && v.chests[Chests.SelectedIndex].y != -1)
            {
                XBox.Text = v.chests[Chests.SelectedIndex].X.ToString();
                YBox.Text = v.chests[Chests.SelectedIndex].y.ToString();
            }
            else
            {
                XBox.Text = "";
                YBox.Text = "";
            }
            Items.Enabled = false;
            if (XBox.Text != "" || YBox.Text != "")
            {
                Items.Enabled = true;
            }
            reset = true;
            Items.SelectedIndex = -1;
            ItemChoice.Text = "";
            ItemCount.Text = "";
            reset = false;
        }

        private void Items_SelectedIndexChanged(object sender, EventArgs e)
        {
            isFirst = true;
            if (!reset)
            {
                ItemChoice.Text = v.chests[Chests.SelectedIndex].items[Items.SelectedIndex].name;
                ItemCount.Text = v.chests[Chests.SelectedIndex].items[Items.SelectedIndex].count.ToString();
                ItemChoice.Enabled = true;
                if (ItemCount.Text.Length > 0)
                {
                    ItemCount.Enabled = true;
                }
                else
                {
                    ItemCount.Enabled = false;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ItemChoice_SelectedIndexChanged(object sender, EventArgs e)
        {

            string[] Bad = { "Tools:", "Weapons:", "Defense items:", "Accessories:", "Furniture:", "Objects:", "Construction Materials:", "Gems:" };
            if (!isFirst)
            {
                if (!Array.Exists(Bad, elements => elements.Equals(ItemChoice.Text)))
                {
                    if (ItemChoice.Text.ToString() != "")
                    {
                        v.chests[Chests.SelectedIndex].items[Items.SelectedIndex].name = ItemChoice.Text.ToString();
                        ItemCount.Enabled = true;
                    }
                }else
                {
                    MessageBox.Show("Please Select a valid option");
                }
            }
                isFirst = false;
        }

        private void ItemCount_TextChanged(object sender, EventArgs e)
        {
            if (ItemCount.Text.Length > 0)
            {
                v.chests[Chests.SelectedIndex].items[Items.SelectedIndex].count = Convert.ToInt32(ItemCount.Text);
            }
        }
        private void X_TextChanged(object sender, EventArgs e)
        {
            if (XBox.Text.Length > 0)
            {
                v.chests[Chests.SelectedIndex].X = Convert.ToInt32(XBox.Text);
            }
            if (YBox.Text.Length > 0)
            {
                Items.Enabled = true;
                v.chests[Chests.SelectedIndex].isEnabled = true;
            }
        }
        private void Y_TextChanged(object sender, EventArgs e)
        {
            if (YBox.Text.Length > 0)
            {
                v.chests[Chests.SelectedIndex].y = Convert.ToInt32(YBox.Text);
            }
            if (XBox.Text.Length > 0)
            {
                Items.Enabled = true;
                v.chests[Chests.SelectedIndex].isEnabled = true;
            }
        }

        private void ExportButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialogW = new SaveFileDialog();
            dialogW.Filter = "Terraria Chest File (*.chest)|*.chest";
            dialogW.Title = "Select Chest File";
            dialogW.FileName = string.Format("{0}.chest", System.IO.Path.GetFileNameWithoutExtension(""));
            string filePath = (dialogW.ShowDialog() == DialogResult.OK) ? dialogW.FileName : string.Empty;
            if (filePath == string.Empty)
            {
                return;
            }
            else if (new System.IO.FileInfo(filePath).Extension.ToUpper() != ".CHEST")
            {
                MessageBox.Show("Output File MUST End in .chest", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (FileStream stream = new FileStream(filePath, FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        writer.Write(v.chests[i].isEnabled);
                        if (v.chests[i].isEnabled)
                        {
                            writer.Write(v.chests[i].X);
                            writer.Write(v.chests[i].y);
                            for (int k = 0; k < 20; k++)
                            {
                                writer.Write(v.chests[i].items[k].count);
                                if (v.chests[i].items[k].count > 0)
                                {
                                    writer.Write(v.chests[i].items[k].name);
                                }
                            }
                        }
                    }
                    writer.Close();
                }
                stream.Close();

            }
        }

        private void ImportButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialogW = new OpenFileDialog();
            dialogW.Filter = "Terraria Chest File (*.chest)|*.chest";
            dialogW.Title = "Select Chest File";
            dialogW.FileName = string.Format("{0}.chest", System.IO.Path.GetFileNameWithoutExtension(""));
            string filePath = (dialogW.ShowDialog() == DialogResult.OK) ? dialogW.FileName : string.Empty;
            if (filePath == string.Empty)
            {
                return;
            }
            else if (new System.IO.FileInfo(filePath).Extension.ToUpper() != ".CHEST")
            {
                MessageBox.Show("Output File MUST End in .chest", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        bool active = reader.ReadBoolean();
                        v.chests[i].isEnabled = active;
                        if (active)
                        {
                            v.chests[i].X = reader.ReadInt32();
                            v.chests[i].y = reader.ReadInt32();
                            for (int k = 0; k < 20; k++)
                            {
                                v.chests[i].items[k].count = reader.ReadInt32();
                                if (v.chests[i].items[k].count > 0){
                                    v.chests[i].items[k].name = reader.ReadString();
                                }
                            }
                        }
                    }
                    reader.Close();
                }
                stream.Close();

            }
        }

        private void ItemChoice_MouseClick(object sender, MouseEventArgs e)
        {
            isFirst = false;
        }
    }
}
